<?php
class Model_MapperAssociation implements Model_IMapper
{
	private $table = null;
	
	public function __construct($str_table, $pdo)
	{
		$this->table = new Model_TableAssociation();
		$this->table->setConnexion($pdo);
		$this->table->init();
	}
	
	public function getAll($begin = null, $nb = null)
	{
                if(is_null($begin) && is_null($nb))
		{
			$lignes = $this->table->fetchAll();
		}
		else
		{
			$lignes = $this->table->fetchAllLimit($begin, $nb);
		}
                
		$results = array();
		foreach($lignes as $ligne)
		{
			$entity = new Model_Association();
			$entity->setId_association($ligne['id_association']);
			$entity->setNom($ligne['nom']);
			$entity->setDescription($ligne['description']);
			$entity->setTelephone($ligne['telephone']);
			$entity->setFax($ligne['fax']);
			$entity->setEmail($ligne['email']);
			$entity->setResponsable($ligne['responsable']);
                        $entity->setLogin($ligne['login']);
                        $entity->setPassword($ligne['password']);
			
			//recupere l'adresse
			$adressemap = new Model_MapperAdresse("adresse", $this->table->getConnexion());
			$adresse = $adressemap->getOne($ligne['id_adresse']);
			
			if(!is_null($adresse))
				$entity->setAdresse($adresse);
					
			$results[] = $entity;
		}
		return $results;
	}
	
	public function getOne($id)
	{
		$ligne = $this->table->fetchOne($id);
		if(!empty($ligne['id_adresse']) )
		{
			$entity = new Model_Association();
			$entity->setId_association($ligne['id_association']);
			$entity->setNom($ligne['nom']);
			$entity->setDescription($ligne['description']);
			$entity->setTelephone($ligne['telephone']);
			$entity->setFax($ligne['fax']);
			$entity->setEmail($ligne['email']);
			$entity->setResponsable($ligne['responsable']);
                        $entity->setLogin($ligne['login']);
                        $entity->setPassword($ligne['password']);
			
			//recupere l'adresse
			$adressemap = new Model_MapperAdresse("adresse", $this->table->getConnexion());
			$adresse = $adressemap->getOne($ligne['id_adresse']);
			
			if(!is_null($adresse))
				$entity->setAdresse($adresse);
		}
		else 
		{
			$entity = null;
		}
		return $entity;
	}

        public function getOneByLogin($login)
        {
                $ligne = $this->table->fetchOneByLogin($login);
		if(!empty($ligne['id_association']) )
		{
			$entity = new Model_Association();
			$entity->setId_association($ligne['id_association']);
			$entity->setNom($ligne['nom']);
			$entity->setDescription($ligne['description']);
			$entity->setTelephone($ligne['telephone']);
			$entity->setFax($ligne['fax']);
			$entity->setEmail($ligne['email']);
			$entity->setResponsable($ligne['responsable']);
                        $entity->setLogin($ligne['login']);
                        $entity->setPassword($ligne['password']);

			//recupere l'adresse
			$adressemap = new Model_MapperAdresse("adresse", $this->table->getConnexion());
			$adresse = $adressemap->getOne($ligne['id_adresse']);

			if(!is_null($adresse))
				$entity->setAdresse($adresse);
		}
		else
		{
			$entity = null;
		}
		return $entity;
        }
	
	public function save(Model_IEntity $object)
	{
		if($object->getId_association() == -1)
		{
			$id = $this->table->insert(
					$object->getNom(),
					$object->getDescription(),
					$object->getTelephone(),
					$object->getFax(),
					$object->getEmail(),
					$object->getResponsable(),
                                        $object->getLogin(),
                                        $object->getPassword(),
					$object->getAdresse()->getId_adresse()
			);
			$object->setId_association($id);
		}
		else
		{
			$this->table->update(
					$object->getId_association(),
					$object->getNom(),
					$object->getDescription(),
					$object->getTelephone(),
					$object->getFax(),
					$object->getEmail(),
					$object->getResponsable(),
                                        $object->getLogin(),
                                        $object->getPassword(),
					$object->getAdresse()->getId_adresse()
			);
		}
	}
	
	public function delete(Model_IEntity $object)
	{
		$this->table->delete($object->getId_association());
	}
}